//
//  FitterViewController.m
//  FinancialProduct
//  筛选
//  Created by Stella on 2017/8/28.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "DBHelper.h"
#import "Header.h"
#import "UIColor+NN.h"
#import "FitterViewController.h"
#import "ZLDatePickerView.h"

@interface FitterViewController ()<ZLPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *timeBegin;
@property (weak, nonatomic) IBOutlet UIButton *timeEnd;
@property (weak, nonatomic) IBOutlet UIButton *timeAny;
@property(nonatomic,strong)UIButton *currentButton;
@property (weak, nonatomic) IBOutlet UIButton *cateAny;
@property(nonatomic,strong)UIButton *currentCateBt;
@property (weak, nonatomic) IBOutlet UIButton *fenpei;
@property (weak, nonatomic) IBOutlet UIButton *qita;
@property (weak, nonatomic) IBOutlet UIButton *chongzhiBtn;

@end

@implementation FitterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentButton=self.timeAny;
    self.currentButton.selected=YES;
    
    self.currentCateBt=self.cateAny;
    self.currentCateBt.selected=YES;
    
    if (_isTouZhi) {
        _fenpei.hidden = YES;
        _qita.hidden = YES;
    }
}
- (IBAction)chooseTime:(UIButton *)sender {
    self.currentButton.selected=NO;
    if (sender==self.timeEnd||sender==self.timeBegin) {
       
        self.currentButton=sender;
        self.timeEnd.selected=YES;
        self.timeBegin.selected=YES;
        self.currentButton.selected=YES;
        
        ZLDatePickerView *date = [[ZLDatePickerView alloc]init];
        date.delegate = self;
        date.isContentTime= _isTouZhi;
        [date show];
    }else{
     
        self.timeEnd.selected=NO;
        self.timeBegin.selected=NO;
        self.currentButton=sender;
        self.currentButton.selected=YES;
    }
    
    if (_isTouZhi) {
        if ([sender.titleLabel.text isEqualToString:@"不限"] && [self.currentCateBt.titleLabel.text isEqualToString:@"不限"]) {
            [_chongzhiBtn setBackgroundImage:[UIImage imageNamed:@"btn3"] forState:UIControlStateNormal];
            [_chongzhiBtn setTitleColor:[UIColor colorWithHexString:@"#676869"] forState:UIControlStateNormal];
        }
        else {
            [_chongzhiBtn setBackgroundImage:[UIImage imageNamed:@"btn2"] forState:UIControlStateNormal];
            [_chongzhiBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
        }
    }
}
- (IBAction)chooseCategory:(UIButton *)sender {
    self.currentCateBt.selected=NO;
    self.currentCateBt=sender;
    self.currentCateBt.selected=YES;
    
    if (_isTouZhi) {
        if ([sender.titleLabel.text isEqualToString:@"不限"] && [self.currentButton.titleLabel.text isEqualToString:@"不限"]) {
            [_chongzhiBtn setBackgroundImage:[UIImage imageNamed:@"btn3"] forState:UIControlStateNormal];
            [_chongzhiBtn setTitleColor:[UIColor colorWithHexString:@"#676869"] forState:UIControlStateNormal];
        }
        else {
            [_chongzhiBtn setBackgroundImage:[UIImage imageNamed:@"btn2"] forState:UIControlStateNormal];
            [_chongzhiBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
        }
    }
}

- (IBAction)reset:(id)sender {
    self.timeEnd.selected=NO;
    self.timeBegin.selected=NO;
    self.currentCateBt.selected=NO;
    self.currentButton.selected=NO;
    self.currentButton=self.timeAny;
    self.currentCateBt=self.cateAny;
    self.currentCateBt.selected=YES;
    self.currentButton.selected=YES;
    
    [_chongzhiBtn setBackgroundImage:[UIImage imageNamed:@"btn3"] forState:UIControlStateNormal];
    [_chongzhiBtn setTitleColor:[UIColor colorWithHexString:@"#676869"] forState:UIControlStateNormal];
}
- (IBAction)OK:(id)sender {
    NSString *timeChoose=self.currentButton.titleLabel.text;
    if (self.currentButton==self.timeEnd||self.currentButton==self.timeBegin) {
        
//        NSDate *dateTime =  [DBHelper dateFromCustomString:roseMode.DocDate format:@"yyyy/MM/dd HH:mm:ss"];
//        NSString *time = [DBHelper stringFromCustomFormatDate:dateTime format:@"yyyy-MM-dd"];

        NSDate *dateStr1 =[DBHelper dateFromCustomString:self.timeBegin.titleLabel.text format:@"yyyy.MM.dd"];
     
        NSDate *dateStr2 =[DBHelper dateFromCustomString:self.timeEnd.titleLabel.text format:@"yyyy.MM.dd"];
    
        NSString *str1 = [DBHelper stringFromCustomFormatDate:dateStr1 format:@"yyyyMMdd"];
        NSString *str2 = [DBHelper stringFromCustomFormatDate:dateStr2 format:@"yyyyMMdd"];
        int res =  [str1 compare:str2];
        switch (res)
        {
            case -1:
            {
                if (_isTouZhi) {
                    str1 = [DBHelper stringFromCustomFormatDate:dateStr1 format:@"yyyy-MM-dd"];
                    str2 = [DBHelper stringFromCustomFormatDate:dateStr2 format:@"yyyy-MM-dd"];
                    timeChoose=[NSString stringWithFormat:@"%@$%@",str1,str2];
                }
                else {
                   timeChoose=[NSString stringWithFormat:@"%@,%@",str1,str2];
                }
                NSLog(@"小于。。。");
            }
                break;
            case 0:{
                NSLog(@"等于。。。");
                if (_isTouZhi) {
                    str1 = [DBHelper stringFromCustomFormatDate:dateStr1 format:@"yyyy-MM-dd"];
                    str2 = [DBHelper stringFromCustomFormatDate:dateStr2 format:@"yyyy-MM-dd"];
                    timeChoose=[NSString stringWithFormat:@"%@$%@",str1,str2];
                }
                else {
                    timeChoose=[NSString stringWithFormat:@"%@,%@",str1,str2];
                }
            }
                
                break;
            case 1:{
                NSLog(@"大于。。。");
                if (_isTouZhi) {
                    str1 = [DBHelper stringFromCustomFormatDate:dateStr1 format:@"yyyy-MM-dd"];
                    str2 = [DBHelper stringFromCustomFormatDate:dateStr2 format:@"yyyy-MM-dd"];
                    timeChoose=[NSString stringWithFormat:@"%@$%@",str1,str2];
                }
                else {
                    timeChoose=[NSString stringWithFormat:@"%@,%@",str2,str1];
                }
            }
                break;
        }
        
        
    }
    NSString *cateChooseS=self.currentCateBt.titleLabel.text;
    
    self.returnFilters(timeChoose,cateChooseS);
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if (point.y<Main_Screen_Height-363.5) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
#pragma mark - DatePicker delegate

-(void)datePickerView:(ZLDatePickerView *)pickView didSelectDate:(NSDate *)date
{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy.MM.dd"];
    NSString *dateStr = [format stringFromDate:date];
    if (self.currentButton==self.timeBegin) {
        [self.timeBegin setTitle:dateStr forState:UIControlStateNormal];
       
    }else{
        [self.timeEnd setTitle:dateStr forState:UIControlStateNormal];
      
    }
}

@end
